* A18a and b : Year-by-year attrition dummy ex-ante

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

drop if firstt_2009==1

drop if add_sample_w2==1|add_sample_w5==1

* Creating exit dummy - if someone filled out all previous years but not this year.
** gen exit variable, replace = missing for all following years if person has attrited in year y

gen t1=0
replace t1=1 if originaltvillage==1

sort id wave
gen exit = 0
bys id : replace exit = 1 if wave != (wave[_n-1] + 1)
replace exit=0 if wave==0

bys id : replace exit = 1 if problem[_n+1]==1

bys id : gen exited = sum(exit)
gen lag_exited=exited[_n-1]

replace exit = . if (exited & lag_exited)

** Now, running t3 with exit as the dependent variable

replace flyer_hyv=1 if flyer_hyv_exp==1

** Don't include flyer_exp in marketing assignment

local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

log using ../res/ex-ante-attrition, replace

/* OLS */


local col=1
	di "`table'"
	
	reg exit year2006-year2013  t1 firstt_2007 lost_w3 resurveyed , r  cluster(villageno)
	test t1 firstt_2007 
	estimates store cT`table'C`col++'	
	predict ols_all_treat_v, xb
	
	reg exit `uniqinst' year2006-year2013  t1 firstt_2007 lost_w3 resurveyed , r  cluster(villageno)
	test `uniqinst' t1 firstt_2007 
	estimates store cT`table'C`col++'	
	predict ols_all_treat_i, xb
	
local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

/* PROBIT */

	probit exit year2006-year2013  t1 firstt_2007 lost_w3 resurveyed , r  cluster(villageno)
	test t1 firstt_2007 
	estimates store cT`table'C`col++'	
	predict probit_all_treat_v, pr

	probit exit `uniqinst' year2006-year2013  t1 firstt_2007 lost_w3 resurveyed , r  cluster(villageno)
	test `uniqinst' t1 firstt_2007 
	estimates store cT`table'C`col++'	
	predict probit_all_treat_i, pr


	qui estout cT`table'C* using "../out/ea-exit-t3.csv", replace ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	mlabel("" "" "" "") ///
	label stats( N)


/* IV REGRESSIONS */
local col=1
	di "`table'"	

local uniqinst "assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

	* Village IV (No Fixed Effects) 
	ivreg2 exit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed, r cluster(villageno)
	test s_policy_units firstt_2007
	estimates store cT`table'C`col++'
	predict ols_attrit_vill, xb 

	/* IND IV (No Fixed Effects) */
	ivreg2 exit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed, r  cluster(villageno)
	test s_policy_units firstt_2007
	estimates store cT`table'C`col++'	
	predict ols_attrit_ind, xb
	
	qui estout cT`table'C* using "../out/ea-exit-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	label stats(cdf N)


local col=1
	di "`table'"
	
	*Probits 
	ivprobit exit (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed, r  cluster(villageno)
	test s_policy_units firstt_2007
	estimates store cT`table'C`col++' 

	predict attrit_vill, pr 
	
	ivprobit exit (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed, r  cluster(villageno)
	test s_policy_units firstt_2007
	estimates store cT`table'C`col++'
	
	predict attrit_ind, pr

	qui estout cT`table'C* using "../out/ea-exit-t3.csv", append ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	label stats(chi2 N)
	
la var ols_attrit_vill "Village level IV - OLS"
la var ols_attrit_ind "Individual level IV - OLS"
la var attrit_vill "Village level IV - probit"
la var attrit_ind "Ind level IV - probit"
la var ols_all_treat_v "Village Reduced form regression - OLS"
la var probit_all_treat_v "Village Reduced form regression - probit"
la var ols_all_treat_i "Indiv Reduced form regression - OLS"
la var probit_all_treat_i "Indiv Reduced form regression - probit"

foreach var of varlist ols_attrit_vill ols_attrit_ind attrit_vill attrit_ind ols_all_treat_v ols_all_treat_i  probit_all_treat_v probit_all_treat_i  {
sum `var'
local l`var': variable label `var'
hist `var', percent xtitle("predicted attrition rate") ytitle("percent") title("`l`var''")
graph save ../out/`var'.gph, replace
}

grc1leg ../out/ols_all_treat_v.gph  ../out/ols_all_treat_i.gph ../out/probit_all_treat_v.gph ../out/probit_all_treat_i.gph ../out/ols_attrit_vill.gph ../out/ols_attrit_ind.gph ../out/attrit_vill.gph ../out/attrit_ind.gph , graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../out/exit-attrition-exante.emf", replace

gen inv_attrit_v = 1/attrit_vill
gen inv_attrit_i = 1/attrit_ind
gen inv_ols_att_v = 1/ols_attrit_vill
gen inv_ols_att_i = 1/ols_attrit_ind
gen inv_ols_all_treat_v = 1/ols_all_treat_v
gen inv_probit_all_treat_v = 1/probit_all_treat_v
gen inv_ols_all_treat_i = 1/ols_all_treat_i
gen inv_probit_all_treat_i = 1/probit_all_treat_i


local outcomevars "totalrevenues_w1 kval_w1 fin_revenues totalcosts_w1 cost_k_w1 b110rk_w1 b111rk_w1 b112pk_w1 fin_costs totalprofit_w1 profit_k_w1 "

foreach var of local outcomevars {
gen `var'_real = `var'/deflator
}

log close 

* a18a
keep if bal_panel3==1

local uniqinst "discount_2007 groupT muslimT hinduT video ppayT vframeT pframeT assigned_risk_ws_2008 discount_2008 rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv bdmperc_2009 fourbdmperc_2009 disc4game_2009 bdmperc_2010 fourbdmperc_2010 disc4game_2010 assigned_risk_ws_2010 assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan bdmperc_2011 fourbdmperc_2011 disc4game_2011 bdmperc_2012 fourbdmperc_2012 disc4game_2012 bdmperc_2013 fourbdmperc_2013 disc4game_2013"  /* sewaT peerT assigned_risk_ws_2011 assigned_risk_ws_2012 assigned_risk_ws_2013 mrkt_allnegative mrkt_poslang mrkt_posimg*/

foreach var of varlist `uniqinst' {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}

foreach var of varlist `uniqinst' {
	replace `var' = 0 if treat_year==0
}

drop if id==111246 | id==113474
local table = 1
local rastring "replace"
local outcomevars "totalrevenues_w1_real kval_w1_real fin_revenues_real totalcosts_w1_real cost_k_w1_real b110rk_w1_real b111rk_w1_real b112pk_w1_real total_mandays_k_w1 total_hired_mandays_k_w1 total_fam_mandays_k_w1 fin_costs totalprofit_w1_real profit_k_w1_real  fraction_hy fraction_cashcrop plotsize_ha"
foreach var of local outcomevars {
replace `var' = 0 if `var' == .
}

xtset id mkt_year

*a18a
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_attrit_v] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_attrit_v] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed [pw=inv_attrit_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed [pw=inv_attrit_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/ea-t3-invp-ivprobit.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 

local rastring "append"

}

*a18b
foreach ovar of varlist `outcomevars' {
local col=1
	di "`ovar'"
	di "`table'"

	/* Village IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_probit_all_treat_v] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* Village IV(FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year) year2*  firstt_2007 lost_w3 resurveyed [pw=inv_probit_all_treat_v] , fe cluster(villageno)
	estimates store cT`table'C`col++'
	
	/* IND IV (No Fixed Effects) */
	ivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed [pw=inv_probit_all_treat_i] , r cluster(villageno) 
	estimates store cT`table'C`col++'
	
	/* IND IV (Individual FE/RE) */
	xtivreg2 `ovar' (s_policy_units=treat_year `uniqinst') year2*  firstt_2007 lost_w3 resurveyed [pw=inv_probit_all_treat_i], fe cluster(villageno)
	estimates store cT`table'C`col++'	

	qui estout cT`table'C* using "../out/ea-t3-invp-probit.csv", ///
	cells(b(star fmt(3)) se(par(`"="("' `")""'))) stardetach  delimiter(",") ///
	starlevels(* .1 ** .05 *** .01) ///
	keep (s_policy_units) prehead("`ovar'") ///
	mlabel("" "" "" "") ///
	`rastring' label stats(cdf N) 
	
local rastring "append"

}

